//  Android Carpool System
//  2011.5-2011.5
//  Author Jinhuan Wang
//  name: LogonView.java
//  description: provide a sign in view 
//$history: /carpool/scr/com/carpool/LogonView.java$
package com.carpool;

import com.carpool.message.Signin;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class LogonView extends Activity {
	AlertDialog.Builder adb;
	final String tag = "LogonView";
	UserInfo uinfo = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.logonview);
		this.adb = new AlertDialog.Builder(this);
		this.uinfo = new UserInfo(this.getApplicationContext());
		final Button signinbutton = (Button) findViewById(R.id.l_LogOn);
		signinbutton.setOnClickListener(new Button.OnClickListener() {

			public void onClick(View v) {
				String Sname = null;
				String Spassword = null;
				try {
					final EditText Ename = (EditText) findViewById(R.id.UserName_edit);
					Sname = Ename.getText().toString();
					if (Ename.getText().length() == 0) {
						AlertDialog ad = LogonView.this.adb.create();
						ad.setMessage("Please Enter a Username");
						ad.show();
						return;
					}
					uinfo.setUsername(Sname);
					final EditText Epassword = (EditText) findViewById(R.id.PassWord_edit);
					Spassword = Epassword.getText().toString();
					if (Epassword.getText().length() == 0) {
						AlertDialog ad = LogonView.this.adb.create();
						ad.setMessage("Please Enter a Password");
						ad.show();
						return;
					}
					

					if (Signin.signin(Sname, Spassword, uinfo))
						{

						try {

							startActivity(new Intent(v.getContext(),
									ViewMap.class));
						} catch (Exception e) {
							Log.i(LogonView.this.tag,
									"Failed to Launch ViewMap ["
											+ e.getMessage() + "]");
						}
						finish();

					} else {
						AlertDialog ad = LogonView.this.adb.create();
						ad.setMessage("Invalid Username or Password");
						ad.show();
						return;
					}
				}

				catch (Exception e) {
					Log.i(LogonView.this.tag,
							"Failed to Logon [" + e.getMessage() + "]");
				}
			}
		});
		final Button signupbutton = (Button) findViewById(R.id.l_Register);
		signupbutton.setOnClickListener(new Button.OnClickListener() {

			public void onClick(View v) {
				try {

					startActivity(new Intent(v.getContext(),
							SignupView.class));
				} catch (Exception e) {
					Log.i(LogonView.this.tag,
							"Failed to Launch ViewMap ["
									+ e.getMessage() + "]");
				}
				finish();
			}
		});
		final Button cancelbutton = (Button) findViewById(R.id.l_Cancel);
		cancelbutton.setOnClickListener(new Button.OnClickListener() {

			public void onClick(View v) {
				finish();
			}

		});
	}
}
